#include "StdAfx.h"

EditBox::EditBox(ui32 x, ui32 y, ui32 w, ui32 h) : m_text("Fonts\\FrizQT__.ttf", "Friz Quadrata TT", 6, 12), mUseUnicode(false)
{
	m_position.x = (float)x;
	m_position.y = (float)y;
	m_size.x = (float)w;
	m_size.y = (float)h;
	m_curText = "";

	m_vertices[0].x = m_position.x;
	m_vertices[0].y = m_position.y;
	m_vertices[0].u = 0.0f;
	m_vertices[0].v = 0.0f;

	m_vertices[1].x = m_position.x + m_size.x;
	m_vertices[1].y = m_position.y;
	m_vertices[1].u = 1.0f;
	m_vertices[1].v = 0.0f;

	m_vertices[2].x = m_position.x + m_size.x;
	m_vertices[2].y = m_position.y + m_size.y;
	m_vertices[2].u = 1.0f;
	m_vertices[2].v = 20.0f / 32.0f;

	m_vertices[3].x = m_position.x;
	m_vertices[3].y = m_position.y + m_size.y;
	m_vertices[3].u = 0.0f;
	m_vertices[3].v = 20.0f / 32.0f;

	m_linePoints[0].x = m_position.x + 6;
	m_linePoints[0].y = m_position.y + 5;
	m_linePoints[1].x = m_position.x + 7;
	m_linePoints[1].y = m_position.y + 5;
	m_linePoints[2].x = m_position.x + 7;
	m_linePoints[2].y = m_position.y + m_size.y - 5;
	m_linePoints[3].x = m_position.x + 6;
	m_linePoints[3].y = m_position.y + m_size.y - 5;
	m_linePoints[4] = m_linePoints[0];
	
	for(ui32 i = 0; i < 5; ++i)
		m_linePoints[i].dwColor = 0xFFFFFFFF;
	
	m_border = CTextureHandler::GetInstance()->GetTexture("Interface\\COMMON\\Common-Input-Border.blp");
	m_focus = false;
	m_drawCursel = true;

	m_lastChange = 0;
}

void EditBox::Draw()
{
	if(!m_visible)
		return;
	sD3D->SetFVF(VERTEX3FRHWT);
	sD3D->SetTexture(0, *m_border);
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_vertices, sizeof(Vertex3FRHWT));
	if(!mUseUnicode)
		m_text.DrawTextA(ui32(m_position.x + 5), ui32(m_position.y + 4), m_text.GetTextLen(m_curText, false) + 10, 40, m_curText);
	else
		m_text.DrawTextW(ui32(m_position.x + 5), ui32(m_position.y + 4), m_text.GetTextLenW(m_wcurText, false) + 10, 40, m_wcurText);
	if(!m_lastChange)
	{
		m_drawCursel = true;
		m_lastChange = GetTickCount();
	}
	else
	{
		ui32 now = GetTickCount();
		if(now - m_lastChange >= 1000 && m_drawCursel)
		{
			m_drawCursel = !m_drawCursel;
			m_lastChange = now;
		}
		else if(now - m_lastChange >= 750 && !m_drawCursel)
		{
			m_drawCursel = !m_drawCursel;
			m_lastChange = now;
		}
	}

	if(m_drawCursel && m_focus)
	{
		sD3D->SetTexture(0, 0);
		sD3D->SetFVF(VERTEX3FRHWC);
		sD3D->DrawPrimitiveUP(D3DPT_LINESTRIP, 4, m_linePoints, sizeof(Vertex3FRHWC));
	}
}

void EditBox::OnInput(char c)
{
	if(!m_focus)
		return;

	if(c == '\b')
	{
		if(m_curText.length())
			m_curText.resize(m_curText.size() - 1);
		for(ui32 i = 0; i < 5; ++i)
			m_linePoints[i].x = m_position.x + 6 + m_text.GetTextLen(m_curText, true);
		return;
	}
	if(c < 20)
		return;

	std::string newString = m_curText;
	newString += c;
	ui32 textLen = m_text.GetTextLen(newString, true);
	if(textLen > m_size.x - 10)
		return;
	m_curText += c;
	for(ui32 i = 0; i < 5; ++i)
		m_linePoints[i].x = m_position.x + 6 + m_text.GetTextLen(m_curText, true);
}